package p.ithorns.framework.actuator.logview;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * MyControllerAdvice
 *
 * @author ithorns
 * @version 1.0.0
 * @date 2023/3/13 18:14
 */
@RestControllerAdvice
public class ErrorAdvice {

    // 当发生异常时，进行统一处理
    @ExceptionHandler(value = {Exception.class})
    public Map handleException(HttpServletRequest request, HttpServletResponse response, Exception e) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", e.getMessage());
        map.put("code", 500);
        //request.setAttribute("javax.servlet.error.status_code", 500);// 设置状态码
        // return "forward:/error";// 将请求交给springboot的默认error处理器
        return map;
    }

}